package com.shujia.core

import java.time.Duration

import org.apache.flink.api.common.eventtime.{SerializableTimestampAssigner, WatermarkStrategy}
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows
import org.apache.flink.streaming.api.windowing.time.Time

object Demo6EventTime {
  def main(args: Array[String]): Unit = {
    /**
      * 事件时间，数据中自带的时间字段，可以反应数据真实发生的时间
      *
      */


    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    env.setParallelism(1)

    //设置时间模式为事件时间
    //env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)


    //读取时间
    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)


    //取出时间字段,时间字段需要转换成long类型
    val eventTimeDS: DataStream[(String, Long)] = linesDS.map(line => {
      val split: Array[String] = line.split(",")
      (split(0), split(1).toLong)
    })


    //告诉flink 哪一个列是时间字段, 水位线默认等于最新一条数据的时间戳
    //val assDS: DataStream[(String, Long)] = eventTimeDS.assignAscendingTimestamps(_._2)

    /**
      * 设置水位线生产策略和指定时间字段
      *
      */
   val assDS: DataStream[(String, Long)] =  eventTimeDS.assignTimestampsAndWatermarks(
      WatermarkStrategy
        //水位线前移5秒，最大允许数据乱序5秒，窗口延迟5秒计算
        .forBoundedOutOfOrderness[(String, Long)](Duration.ofSeconds(5))
        //指定时间字段
        .withTimestampAssigner(
        new SerializableTimestampAssigner[(String, Long)] {
          override def extractTimestamp(element: (String, Long), recordTimestamp: Long): Long = {
            //时间字段
            element._2
          }
        })
    )


    //统计最近5秒单词的数量
    val kvDS: DataStream[(String, Int)] = assDS.map(kv => (kv._1, 1))

    kvDS
      .keyBy(_._1)
      //滚动的事件时间的窗口
      .window(TumblingEventTimeWindows.of(Time.seconds(5)))
      .sum(1)
      .print()

    env.execute()

  }

}
